package com.example.housekeeping.controller;

import com.example.housekeeping.mapper.BannerMapper;
import com.example.housekeeping.pojo.entity.Banner;
import com.example.housekeeping.pojo.vo.BannerStandardVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.util.List;

@RestController
@RequestMapping("/banner/")
public class BannerController {
    //TODO 路径后续待补全
    @Value("${dirPath}")
    private String dirPath;

    @Autowired(required = false)
    BannerMapper mapper;
    @RequestMapping("select")
    public List<Banner> select(){
        return mapper.select();
    }
    @RequestMapping("delete")
    public void delete(Long id){
        BannerStandardVO banner = mapper.selectById(id);
        new File(dirPath+banner.getUrl()).delete();

        mapper.deleteById(id);
    }
    @RequestMapping("insert")
    public void insert(Banner url){
        System.out.println("url = " + url);
        mapper.insert(url);

    }
    @RequestMapping("selectById")
    public BannerStandardVO selectById(Long id){
        return mapper.selectById(id);
    }
    @RequestMapping("update")
    public void update(Banner banner){
        BannerStandardVO oldBanner = mapper.selectById(banner.getId());
        //删除原来的图片文件

        new File("d:/files"+oldBanner.getUrl()).delete();
        mapper.update(banner);
    }
}
